package com.wgl.yygh.serivce.impl;

import com.alibaba.fastjson.JSONObject;
import com.wgl.model.vo.hosp.DepartmentVo;
import org.springframework.data.domain.Page;
import com.wgl.model.hosp.Department;
import com.wgl.model.vo.hosp.DepartmentQueryVo;
import com.wgl.yygh.repository.DepartmentRepository;
import com.wgl.yygh.serivce.DepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wgl
 * @version 1.0.0
 * @ClassName DepartmentServiceImpl.java
 * @Description TODO
 * @createTime 2022年02月06日 14:11:00
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> map) {
        String s = JSONObject.toJSONString(map);
        Department department = JSONObject.parseObject(s,Department.class);
        Department departmentExcite = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        department.setIsDeleted(0);
        department.setUpdateTime(new Date());
        //p判断是否存在 存在即修改 不存在就添加
        if (departmentExcite == null){
            //添加
            department.setCreateTime(new Date());
        } else {
            department.setId(departmentExcite.getId());
        }
        departmentRepository.save(department);
    }

    @Override
    public Page<Department> findPageDepartment(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
        //创建分页参数Pageable
        Pageable pageable = PageRequest.of(page-1,limit);
        //
        ExampleMatcher matcher =  ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        Example<Department> example = Example.of(department,matcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);

        return all;
    }

    @Override
    public void remove(String hospcode, String depcode) {
        //先查询科室 根据医院编号和科室编号

        Department departmentByHoscodeAndDepcode = departmentRepository.getDepartmentByHoscodeAndDepcode(hospcode, depcode);

        if (departmentByHoscodeAndDepcode != null){
            //删除
            departmentRepository.deleteById(departmentByHoscodeAndDepcode.getId());
        }
    }

    @Override
    public List<DepartmentVo> findeDeptTree(String hoscode) {

        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> all = departmentRepository.findAll(example);
        //将list对大科室编号进行分类
        Map<String, List<Department>> collect = all.stream().collect(Collectors.groupingBy(Department::getBigcode));

        List<DepartmentVo> list = new ArrayList<>(collect.size());

        for (Map.Entry<String, List<Department>> entry : collect.entrySet()) {
            //得到大科室编号
            String key = entry.getKey();

            //得到所有的子科室列表
            List<Department> value = entry.getValue();

            //封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(key);
            departmentVo.setDepname(value.get(0).getBigname());
            System.out.println(departmentVo);

            //封装小科室
            List<DepartmentVo> objects = new ArrayList<>(value.size());
            DepartmentVo departmentVo1 = null;
            for (Department department1 : value) {
                departmentVo1 = new DepartmentVo();
                departmentVo1.setDepcode(department1.getDepcode());
                departmentVo1.setDepname(department1.getDepname());
                objects.add(departmentVo1);
            }
            departmentVo.setChildren(objects);
            list.add(departmentVo);
        }
        return list;
    }

    @Override
    public String getDepName(String  hoscode,String depcode) {

        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode).getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);

    }
}
